//
//  SceneDelegate+ZJ.m
//  iosFramework
//
//  Created by Farben on 2020/8/5.
//  Copyright © 2020 Farben. All rights reserved.
//

#import "SceneDelegate+ZJ.h"
#import "ZJRTRootNavigationController.h"
#import "ZJTabBarViewController.h"
#import <AvoidCrash/AvoidCrash.h>
#import <Bugly/Bugly.h>
#import "ZJMacroFile.h"

@implementation SceneDelegate (ZJ)

-(void)startAvoidCrash{
    //若需要防止某个前缀的类的unrecognized selector sent to instance
    //比如你所开发项目中使用的类的前缀:CC、DD
    //你可以调用如下方法
//    NSArray *noneSelClassPrefix = @[
//                                    @"CC",
//                                    @"DD"
//                                    ];
//    [AvoidCrash setupNoneSelClassStringPrefixsArr:noneSelClassPrefix];
//
//
//
//    //监听通知:AvoidCrashNotification, 获取AvoidCrash捕获的崩溃日志的详细信息
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealwithCrashMessage:) name:AvoidCrashNotification object:nil];
    
    [AvoidCrash makeAllEffective];
    
    NSArray *noneSelClassStrings = @[
                          @"NSNull",
                          @"NSNumber",
                          @"NSString",
                          @"NSDictionary",
                          @"NSArray"
                          ];
    [AvoidCrash setupNoneSelClassStringsArr:noneSelClassStrings];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealwithCrashMessage:) name:AvoidCrashNotification object:nil];
    
}



- (void)initBugly{
    BuglyConfig * config = [[BuglyConfig alloc] init];
    config.reportLogLevel = BuglyLogLevelWarn;
    config.blockMonitorEnable = YES;
    config.blockMonitorTimeout = 10;
    [Bugly startWithAppId:@"id" config:config];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [AvoidCrash makeAllEffective];

    //监听通知:AvoidCrashNotification, 获取AvoidCrash捕获的崩溃日志的详细信息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealwithCrashMessage:) name:AvoidCrashNotification object:nil];
    return YES;
}


#pragma mark - avoidCrash抓取错误信息用bugly上传到后台日志
- (void)dealwithCrashMessage:(NSNotification *)note {
    //异常拦截并且通过bugly上报
    
    NSDictionary *info = note.userInfo;
    NSString *errorReason = [NSString stringWithFormat:@"【ErrorReason】%@========【ErrorPlace】%@========【DefaultToDo】%@========【ErrorName】%@", info[@"errorReason"], info[@"errorPlace"], info[@"defaultToDo"], info[@"errorName"]];
    NSArray *callStack = info[@"callStackSymbols"];
    
//    [BuglyManager reportErrorName:Bugly_ErrorName_AvoidCrash errorReason:errorReason callStack:callStack extraInfo:nil];
}

/** 上报错误信息 */
+ (void)reportErrorName:(NSString *)errorName errorReason:(NSString *)errorReason callStack:(NSArray *)aStackArray extraInfo:(NSDictionary *)info{
    
//    [Bugly reportExceptionWithCategory:3 errorName reason:errorReason callStack:aStackArray extraInfo:info terminateApp:NO];
    [Bugly reportExceptionWithCategory:3 name:@"错误类别❌" reason:@"错误信息" callStack:[NSArray new] extraInfo:[NSDictionary dictionary] terminateApp:YES];
    
}


@end
